package com.qianhe.demo;

import com.github.qianhe.sdk.QHUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
public class QHReadController {

    @Autowired
    private QHReadService readServiceApi;

    @RequestMapping()
    public String index(){
        return "index.html";
    }

    /**
     * 异步通知 任务完成状况 实时通知、10秒、1分钟、1小时 异步通知 4次保证消息送达
     *
     * @return
     */
    @PostMapping("notifyProcess")
    @ResponseBody
    public Map<String, Object> notifyProcess(@RequestBody Map<String, String> requestData) throws Exception {
        System.out.println(QHUtil.Json.toJSONString(requestData));
        Map<String, Object> result = new HashMap<>(2);
        //处理回调 签名验证
        try{
            if (readServiceApi.verifySign(requestData)) {
                //完成 业务处理 发奖励 生成完成任务记录 和奖励记录
                //do something
                // 1、判断是否请求过，避免重复发放佣金
                // 2、集群部署需要加锁 ，避免通知重复
                result.put("code", 200);
                result.put("msg", "成功");
                System.out.println("成功");
                return result;
            }
        }catch (Exception exception){
            exception.printStackTrace();
        }

        //签名失败处理
        result.put("code", -1);
        result.put("msg", "签名失败");
        return result;
    }
    /**
     * 领取任务列表
     *
     * @return
     */
    @PostMapping("getTaskList")
    @ResponseBody
    public Map<String, Object> getTaskList(@RequestBody Map<String,String> requestData) {
        try {
            return readServiceApi.getTaskList(requestData);
        } catch (Exception exception) {
            exception.printStackTrace();
            Map<String, Object> responseResult = new HashMap<>(2);
            responseResult.put("code", -1);
            responseResult.put("msg", exception.getMessage());
            return responseResult;
        }
    }

    /**
     * 一键领取
     *
     * @return
     */
    @PostMapping("getReadTask")
    @ResponseBody
    public Map<String, Object> getReadTask(@RequestBody Map<String,String> requestData)  {
        try {
            return readServiceApi.getReadTask(requestData);
        } catch (Exception exception) {
            exception.printStackTrace();
            Map<String, Object> responseResult = new HashMap<>(2);
            responseResult.put("code", -1);
            responseResult.put("msg", exception.getMessage());
            return responseResult;
        }
    }

    /**
     * 更新异步任务完成通知地址
     *
     * @return
     */
    @PostMapping("updateNotifyUrl")
    @ResponseBody
    public Map<String, Object> updateNotifyUrl(@RequestBody Map<String,String> requestData)  {
        try {
            return readServiceApi.updateNotifyUrl(requestData);
        } catch (Exception exception) {
            exception.printStackTrace();
            Map<String, Object> responseResult = new HashMap<>(2);
            responseResult.put("code", -1);
            responseResult.put("msg", exception.getMessage());
            return responseResult;
        }
    }

}
